package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.service.admin.AdminCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author :rain
 * @description
 * @since 2022/06/27 16:18
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    //获取评论的信息
    @Override
    public CommonData<MarketComment> queryCommentInfo(String userId, String valueId, BasePageInfo info) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        //拼接userId
        if (!StringUtils.isEmpty(userId)){
            try {
                criteria.andUserIdEqualTo(Integer.parseInt(userId));
            }catch (Exception e){
                return null;
            }
        }
        //拼接valueId
        if (!StringUtils.isEmpty(valueId)){
            try {
                criteria.andValueIdEqualTo(Integer.parseInt(valueId));
            }catch (Exception e){
                return null;
            }

        }
        //删除方法deleted为true，false为不删除
        criteria.andDeletedEqualTo(false);
        //获取所有的Comment的信息
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);

        //将list转换成分页的信息进行显示
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);
        return CommonData.data(pageInfo);
    }

    //删除方法本质是修改(将deleted设置为true，表示已经删除)，并不是真正的删除
    @Override
    public Integer deleteInfoById(MarketComment marketComment) {
        marketComment.setDeleted(true);
        int affactRows = marketCommentMapper.updateByPrimaryKey(marketComment);
        return affactRows;
    }
}
